﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Odds.Entity;
using HtmlAgilityPack;
using System.IO;
using System.Text.RegularExpressions;

namespace Odds.Business
{
    public class MatchResultOrganizeMonitor
    {
        const string trClass = "f12_3333";
        const string trBgColor = "#ffffff";

        private List<OddsResult> oddsResultSet = new List<OddsResult>();
        public List<OddsResult> _OddsResultSet
        {
            get { return oddsResultSet; }
        }

        /// <summary>
        /// http://www.eooele.net/cpkjjg/2010.asp?id=20
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="fileType">0: result in the txt file</param>
        public MatchResultOrganizeMonitor(string resultString, int fileType)
        {
            string[] strRow = resultString.Split('\r');
            foreach(string row in strRow)
            {
                string[] strIdResult = row.Split('：');
                int issueRealID = 0;
                if (int.TryParse(Regex.Matches(strIdResult[0], "\\d{7}")[0].ToString().Replace("20",""), out issueRealID))
                {
                    string[] resultList = strIdResult[1].Replace("*", "4").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    oddsResultSet.Add(new OddsResult()
                    {
                        OddsIssueRealID = issueRealID,
                        OddsIssueItemMatchResult = resultList.Select(r => short.Parse(r)).ToList(),
                        Win14 = 0,
                        Win13 = 0,
                        Win9 = 0
                    });
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url">http://act2.sports.sohu.com/zucai/file/zucai-2009-asc.html</param>
        public MatchResultOrganizeMonitor(string url)
        {
            string html = JJY.CommonHelper.HttpRequestService.GetHtmlSourceByUrl(url);


            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);

            HtmlNodeCollection trResultSet =
                doc.DocumentNode.SelectNodes("//tr[@class='" + trClass + "'][@bgcolor='" + trBgColor + "']");

            foreach (HtmlNode nodeTR in trResultSet)
            {
                HtmlNodeCollection tdResultSet = doc.DocumentNode.SelectNodes(nodeTR.XPath + "/td");
                if (tdResultSet.Count == 19)
                {
                    int issueRealID = 0;
                    if (doc.DocumentNode.SelectNodes(tdResultSet[0].XPath + "/a") != null
                        && int.TryParse(doc.DocumentNode.SelectNodes(tdResultSet[0].XPath + "/a")[0].InnerText, out issueRealID)
                        && issueRealID > 0)
                    {
                        List<short> matchResult = new List<short>();
                        for (int i = 1; i < 15; i++)
                        {
                            if (tdResultSet[i].InnerText == "0" || tdResultSet[i].InnerText == "1" || tdResultSet[i].InnerText == "3")
                            {
                                matchResult.Add(short.Parse(tdResultSet[i].InnerText));
                            }
                            else
                            {
                                matchResult.Add(short.Parse("4"));
                            }
                        }

                        int win14 = 0, win13 = 0, win9 = 0;

                        int.TryParse(tdResultSet[15].InnerText, out win14);
                        int.TryParse(tdResultSet[16].InnerText, out win13);
                        int.TryParse(tdResultSet[17].InnerText, out win9);


                        oddsResultSet.Add(new OddsResult()
                        {
                            OddsIssueRealID = issueRealID,
                            OddsIssueItemMatchResult = matchResult,
                            Win14 = win14,
                            Win13 = win13,
                            Win9 = win9
                        });
                    }
                }
            }

        }


        public string ShowResultTable()
        {
            StringBuilder result = new StringBuilder();
            result.Append("<table>");
            foreach (OddsResult r in oddsResultSet)
            {
                result.Append("<tr>");
                result.Append("<td>");
                result.Append(r.OddsIssueRealID.ToString());
                result.Append("</td>");
                result.Append("<td>");
                result.Append(r.Win14.ToString() + " / " + r.Win13.ToString() + " / " + r.Win9.ToString());
                result.Append("</td>");
                result.Append("<td>");
                foreach (short s in r.OddsIssueItemMatchResult)
                {
                    result.Append(s.ToString() + "/");
                }
                result.Append("</td>");
                result.Append("</tr>");
            }
            result.Append("</table>");

            return result.ToString();
        }
    }
}
